package com.jdk17feature;

import java.util.Arrays;
import java.util.List;

/**
 * List.of();Map.of();Set.of()
 * @author piaohailin@163.com
 * @date 2021/9/17
 */
public class Listof {

    //Arrays.asList()可以插入null，而List.of()不可以
    public void solution1() {
        List<Integer> ls1 = Arrays.asList(1, 2, null);
        //List<Integer> ls2 = List.of(1,2,null);
        System.out.println(ls1);
        //System.out.println(ls2);
    }

    //List.of生成的List不能修改，Arrays.asList生成的List能修改
    public void solution2() {
        List<Integer> ls1 = Arrays.asList(1, 2, null);
        List<Integer> ls2 = List.of(1, 2);
        ls1.set(0, 5);
//        ls2.set(0,5);
        System.out.println(ls1);
        System.out.println(ls2);
    }

    //数组修改对Arrays.asList生成的List有影响，对List.of 生成的List无影响
    public void solution3() {
        Integer[] a = new Integer[]{1, 2, 3, 4};
        // 不能用int[]，会导致转型错误，错误: 不兼容的类型: 推论变量 T 具有不兼容的上限
        List<Integer> ls1 = Arrays.asList(a);
        List<Integer> ls2 = List.of(a);
        System.out.println(ls1);
        System.out.println(ls2);
        a[0] = 5;
//        ls2.set(0,5);
        System.out.println(ls1);
        System.out.println(ls2);
    }

    public static void main(String[] args) {
        Listof obj = new Listof();
//        obj.solution1();
//        obj.solution2();
        obj.solution3();
    }
}
